package de.ret_world.openrpgstudio.editor.gui;

import buoy.event.CommandEvent;
import buoy.event.ValueChangedEvent;
import buoy.event.WindowClosingEvent;
import buoy.widget.*;
import de.ret_world.openrpgstudio.editor.Configuration;
import de.ret_world.openrpgstudio.editor.Main;
import de.ret_world.openrpgstudio.editor.helpers.Internationalization;
import java.awt.Insets;
import java.awt.Rectangle;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

/**
 * Dialog for the creation of new Projects. Normally you have nothing todo with this class.
 *
 * @author Emu
 */
public class NewProjectDialog {

    private BFrame parent;
    private Configuration config;
    private BTextField txtProject, txtGame, txtPath;
    private Path projectPath;
    private BDialog dialog;
    private Properties properties;

    public NewProjectDialog(BFrame parent) {
        this.parent = parent;
        config = Configuration.getInstance(Main.PLUGIN_ID);
    }

    /**
     * Shows the New Project Dialog.
     * @return The generated Properties file for the new Project, null if the Dialog was canceled.
     */
    public Properties showDialog() {
        Internationalization intn = Internationalization.getInstance();
        dialog = new BDialog(parent, intn.getString("MENU.NEW"), true);

        LayoutInfo li = new LayoutInfo(LayoutInfo.WEST, LayoutInfo.HORIZONTAL, new Insets(5, 5, 5, 5), null);

        ColumnContainer cc = new ColumnContainer();
        cc.add(new BLabel(intn.getString("NEW.PROJECTNAME")), li);
        txtProject = new BTextField(intn.getString("NEW.NEWPROJECT"));
        txtProject.addEventLink(ValueChangedEvent.class, this, "updateProjectText");
        cc.add(txtProject, li);
        cc.add(new BLabel(intn.getString("NEW.GAMENAME")), li);
        txtGame = new BTextField(intn.getString("NEW.NEWGAME"));
        cc.add(txtGame, li);
        cc.add(new BLabel(intn.getString("NEW.PROJECTPATH")), li);

        projectPath = Paths.get(config.getProperty("project.folder", "Projects")).toAbsolutePath();

        txtPath = new BTextField(projectPath.resolve(txtProject.getText().trim()).toString());
        txtPath.setEditable(false);
        cc.add(txtPath, li);

        BorderContainer bc = new BorderContainer();
        BButton btn = new BButton(intn.getString("OK"));
        btn.addEventLink(CommandEvent.class, this, "ok");
        bc.add(btn, BorderContainer.EAST, li);
        btn = new BButton(intn.getString("CANCEL"));
        btn.addEventLink(CommandEvent.class, this, "cancel");
        bc.add(btn, BorderContainer.CENTER, li);
        btn = new BButton(intn.getString("CHOOSE"));
        btn.addEventLink(CommandEvent.class, this, "choosePath");
        bc.add(btn, BorderContainer.WEST, li);
        cc.add(bc, new LayoutInfo(LayoutInfo.EAST, LayoutInfo.NONE, new Insets(5, 5, 5, 5), null));

        dialog.setContent(cc);
        dialog.addEventLink(WindowClosingEvent.class, this, "cancel");
        dialog.setBounds(new Rectangle(150, 150, 350, 265));
        dialog.setResizable(false);
        dialog.setVisible(true);

        return properties;
    }

    private void updateProjectText() {
        projectPath = Paths.get(config.getProperty("project.folder", "Projects")).toAbsolutePath();
        txtPath.setText(projectPath.resolve(txtProject.getText().trim()).toString());
    }

    private void choosePath() {
        Internationalization intn = Internationalization.getInstance();
        BFileChooser fc = new BFileChooser(BFileChooser.SELECT_FOLDER, intn.getString("MENU.CHOOSEFOLDER"));
        if (fc.showDialog(dialog)) {
            projectPath = fc.getSelectedFile().toPath();
            config.setProperty("project.folder", projectPath.toString());
            txtPath.setText(projectPath.resolve(txtProject.getText().trim()).toString());
        }
    }

    private void cancel() {
        properties = null;
        dialog.dispose();
    }

    private void ok() {
        properties = new Properties();
        properties.setProperty("project.name", txtProject.getText());
        properties.setProperty("game.name", txtGame.getText());
        properties.setProperty("project.path", projectPath.resolve(txtProject.getText()).toString());

        dialog.dispose();
    }
}
